/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package karelia.webpro.controllers;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import karelia.webpro.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Ari Paloniitty
 */
@Controller
@RequestMapping("/user")
public class AccessController {
    
/*@RequestMapping(value = "/accessx")
    public void access(){
        
    }*/

    @RequestMapping(method = RequestMethod.GET)
    public String access(ModelMap model) {
        //this will the data to be viewed in the browser
        List<User> data;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("web-ohj-proj-aaPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        data = em.createNamedQuery("User.findAll").getResultList();
        System.out.println("jotain...");
        for(int i=0; i < data.size(); i++)
            System.out.println(data.get(i));
        em.getTransaction().commit();
        em.close();
        emf.close();
        model.addAttribute("user", data); // assign data to the model
      
        return "access"; // return the name of the view (messages.JSP)
        //return "redirect:access.htm";
    }
    
}

